package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

//@ResponseBody + @ControllerAdvice = @RestControllerAdvice
@ControllerAdvice
public class ExceptionControllerAdvice {

    //使用形参可以接收你抛出的异常对象
    //方法的写法和Handler方法
    @ExceptionHandler({ArithmeticException.class})
    public ModelAndView arithmeticException(ArithmeticException exception) {
        return new ModelAndView("/exception.jsp");
    }

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public BaseRespVo nullPointer(NullPointerException exception) {
        return BaseRespVo.fail("空指针");
    }
}
